home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / filutil / bison110.zip / PRINT.C < prev    next >
C/C++ Source or Header  |  1990-06-26  |  5KB  |  242 lines

  1. /* Print information on generated parser, for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include "system.h"
  23. #include "machine.h"
  24. #include "new.h"
  25. #include "files.h"
  26. #include "gram.h"
  27. #include "state.h"
  28.  
  29.  
  30. extern char **tags;
  31. extern int nstates;
  32. extern short *accessing_symbol;
  33. extern core **state_table;
  34. extern shifts **shift_table;
  35. extern errs **err_table;
  36. extern reductions **reduction_table;
  37. extern char *consistent;
  38. extern char any_conflicts;
  39. extern char *conflicts;
  40.  
  41. extern void conflict_log();
  42. extern void verbose_conflict_log();
  43. extern void print_reductions();
  44.  
  45. void print_token();
  46. void print_state();
  47. void print_core();
  48. void print_actions();
  49.  
  50. void
  51. terse()
  52. {
  53.   if (any_conflicts)
  54.     {
  55.       conflict_log();
  56.     }
  57. }
  58.  
  59.  
  60. void
  61. verbose()
  62. {
  63.   register int i;
  64.  
  65.   if (any_conflicts)
  66.     verbose_conflict_log();
  67.  
  68.   fprintf(foutput, "\n\ntoken types:\n");
  69.   print_token (-1, 0);
  70.   if (translations)
  71.     {
  72.       for (i = 0; i <= max_user_token_number; i++)
  73.     /* Don't mention all the meaningless ones.  */
  74.     if (token_translations[i] != 2)
  75.       print_token (i, token_translations[i]);
  76.     }
  77.   else
  78.     for (i = 1; i < ntokens; i++)
  79.       print_token (i, i);
  80.  
  81.   for (i = 0; i < nstates; i++)
  82.     {
  83.       print_state(i);
  84.     }
  85. }
  86.  
  87.  
  88. void
  89. print_token(extnum, token)
  90. int extnum, token;
  91. {
  92.   fprintf(foutput, " type %d is %s\n", extnum, tags[token]);
  93. }
  94.  
  95.  
  96. void
  97. print_state(state)
  98. int state;
  99. {
  100.   fprintf(foutput, "\n\nstate %d\n\n", state);
  101.   print_core(state);
  102.   print_actions(state);
  103. }
  104.  
  105.  
  106. void
  107. print_core(state)
  108. int state;
  109. {
  110.   register int i;
  111.   register int k;
  112.   register int rule;
  113.   register core *statep;
  114.   register short *sp;
  115.   register short *sp1;
  116.  
  117.   statep = state_table[state];
  118.   k = statep->nitems;
  119.  
  120.   if (k == 0) return;
  121.  
  122.   for (i = 0; i < k; i++)
  123.     {
  124.       sp1 = sp = ritem + statep->items[i];
  125.  
  126.       while (*sp > 0)
  127.     sp++;
  128.  
  129.       rule = -(*sp);
  130.       fprintf(foutput, "    %s  ->  ", tags[rlhs[rule]]);
  131.  
  132.       for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
  133.     {
  134.       fprintf(foutput, "%s ", tags[*sp]);
  135.     }
  136.  
  137.       putc('.', foutput);
  138.  
  139.       while (*sp > 0)
  140.     {
  141.       fprintf(foutput, " %s", tags[*sp]);
  142.       sp++;
  143.     }
  144.  
  145.       fprintf (foutput, "   (rule %d)", rule);
  146.       putc('\n', foutput);
  147.     }
  148.  
  149.   putc('\n', foutput);
  150. }
  151.  
  152.  
  153. void
  154. print_actions(state)
  155. int state;
  156. {
  157.   register int i;
  158.   register int k;
  159.   register int state1;
  160.   register int symbol;
  161.   register shifts *shiftp;
  162.   register errs *errp;
  163.   register reductions *redp;
  164.   register int rule;
  165.  
  166.   shiftp = shift_table[state];
  167.   redp = reduction_table[state];
  168.   errp = err_table[state];
  169.  
  170.   if (!shiftp && !redp)
  171.     {
  172.       fprintf(foutput, "    NO ACTIONS\n");
  173.       return;
  174.     }
  175.  
  176.   if (shiftp)
  177.     {
  178.       k = shiftp->nshifts;
  179.  
  180.       for (i = 0; i < k; i++)
  181.     {
  182.       if (! shiftp->shifts[i]) continue;
  183.       state1 = shiftp->shifts[i];
  184.       symbol = accessing_symbol[state1];
  185.       /* The following line used to be turned off.  */
  186.       if (ISVAR(symbol)) break;
  187.       fprintf(foutput, "    %-4s\tshift, and go to state %d\n",
  188.           tags[symbol], state1);
  189.     }
  190.  
  191.       if (i > 0)
  192.     putc('\n', foutput);
  193.     }
  194.   else
  195.     {
  196.       i = 0;
  197.       k = 0;
  198.     }
  199.  
  200.   if (errp)
  201.     {
  202.       int j, nerrs;
  203.  
  204.       nerrs = errp->nerrs;
  205.  
  206.       for (j = 0; j < nerrs; j++)
  207.     {
  208.       if (! errp->errs[j]) continue;
  209.       symbol = errp->errs[j];
  210.       fprintf(foutput, "    %-4s\terror (nonassociative)\n", tags[symbol]);
  211.     }
  212.  
  213.       if (j > 0)
  214.     putc('\n', foutput);
  215.     }
  216.  
  217.   if (consistent[state] && redp)
  218.     {
  219.       rule = redp->rules[0];
  220.       symbol = rlhs[rule];
  221.       fprintf(foutput, "    $default\treduce using rule %d (%s)\n\n",
  222.                  rule, tags[symbol]);
  223.     }
  224.   else if (redp)
  225.     {
  226.       print_reductions(state);
  227.     }
  228.  
  229.   if (i < k)
  230.     {
  231.       for (; i < k; i++)
  232.     {
  233.       if (! shiftp->shifts[i]) continue;
  234.       state1 = shiftp->shifts[i];
  235.       symbol = accessing_symbol[state1];
  236.       fprintf(foutput, "    %-4s\tgo to state %d\n", tags[symbol], state1);
  237.     }
  238.  
  239.       putc('\n', foutput);
  240.     }
  241. }
  242.